package cn.liyicun.school.car2.controller.user;

import java.util.Date;

import com.jfinal.aop.Clear;
import com.jfinal.captcha.CaptchaRender;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.StrKit;

import cn.liyicun.school.car2.base.BaseController;
import cn.liyicun.school.car2.base.BaseUserController;
import cn.liyicun.school.car2.base.LoginUserName;
import cn.liyicun.school.car2.interceptor.UserLoginInterceptor;
import cn.liyicun.school.car2.model.DriverInfo;
import cn.liyicun.school.car2.model.UserInfo;

@Clear(UserLoginInterceptor.class)
public class UserController extends BaseUserController {

	public void login() {
		System.out.println("--login--------");
	}

	public void login2() {
		System.out.println("--login2--------");
		String username = getPara("username", "").trim().toLowerCase();
		String password = getPara("password", "");
		verification(StrKit.isBlank(username), "用户名不能为空!");
		verification(StrKit.isBlank(password), "密码不能为空!");
		UserInfo data = UserInfo.dao.findFirst("select * from user_info where username=?", username);
		verification(data == null, "用户或密码不对!");
		verification(data.getStatus() != 1, "用户状态不对!");
		verification(!paswordEquals(password, data), "用户或密码不对!");

		LoginUserName data1 = new LoginUserName();
		data1.setId(data.getId());
		data1.setNickname(data.getNickname());
		data1.setPhone(data.getPhone());
		// DriverInfo driver = DriverInfo.dao.findById(data.getId());
		// int isDriver = 0;
		// if (driver != null) {
		// isDriver = driver.getStatus();
		// }
		// data1.setIsDriver(isDriver);
		setLoginAccount(data1);

		// myRenderJson("登陆成功!");
		// redirect301("/line/lineList", false);
		myRenderJsonSuccess("登录成功!");
	}

	public void logout() {
		logOutAccount();
		redirect301("login", false);
	}

	public void register() {
		System.out.println("--register--------");
	}

	public void register2() {

		String username = getPara("username", "").trim().toLowerCase();
		String password = getPara("password", "");
		String phone = getPara("phone", "");
		String nickname = getPara("nickname", "");
		String verify_code = getPara("verify_code", "");
		verification(username.length() < 5, "用户名长度必须5位以上");
		verification(username.length() > 30, "用户名长度必须30位以下");
		verification(!username.matches("^[a-zA-Z].*"), "用户名必须字母开头");
		verification(!username.matches("^[a-zA-Z][a-zA-Z0-9_]*"), "用户名只能为字母数字下划线");
		verification(password.length() < 6, "密码长度必须6位以上");
		verification(nickname.length() < 1, "姓名必须输入!");
		verification(nickname.length() > 30, "姓名长度必须30位以下");
		verification(!phone.matches("1\\d{10}"), "手机号码不对!");
		verificationCaptcha(verify_code);
		UserInfo data = UserInfo.dao.findFirst("select * from user_info where username=?", username);
		verification(data != null, "用户存在!");
		data = new UserInfo();
		data.setCreateTime(new Date());
		data.setNickname(nickname);
		data.setOpenid("1");
		String salt = HashKit.generateSalt(90);
		data.setPassword(buildPasword(password, salt));
		data.setSalt(salt);
		data.setPhone(phone);
		data.setStatus(1);
		data.setUpdateTime(new Date());
		data.setUsername(username.trim().toLowerCase());
		data.save();
		myRenderJsonSuccess("注册成功,请登录!");
		// redirect301("login", false);
	}

	public void captcha() {
		renderCaptcha();
	}

	public void loginCheck() {
		System.out.println("----loginCheck------");
		if (getLoginAccount(false) == null) {
			redirect301("login", false);
		} else {
			redirect301("/line/lineList", false);
		}

	}
}
